import taichi as ti
ti.init(arch=ti.x64) # only CPU backend is supported for now
 

n = 4
# step 1: create sparse matrix builder  构建稀疏矩阵
K = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)  #max_num_triplets最多的非零项

@ti.kernel
def fill(A: ti.linalg.sparse_matrix_builder()):
    for i in range(n):
        A[i, i] += 1  # Only +=  and -= operators are supported for now.

# step 2: fill the builder with data.
fill(K)
A = K.build()


solver = ti.linalg.SparseSolver(solver_type ="LLT")  #分解为 下三角矩阵 * 转置矩阵  的形式
solver.analyze_pattern(A)
solver.factorize(A)

b=809
#solver 
x= solver.solve(b)      # b 可以是 ti.build 也可以是  numpyarray    返回 numpyarray

isSuccessful = solver.info()
print(x)




